﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using DB.SqlHelper;
using Framework.Common.Common;
using Mode;
namespace Framework.DataAccess
{
    public class SetSystem
    {
        private readonly DbInstance db;
        private sysinformation sys = new sysinformation();

        public SetSystem()
        {
            db = DbManager.GetDbInstance();
            get_system_infromation();
            get_administator_infromation();
        }

        // 返回系统的信息 - JSON 格式
        public string GetSystemJSON
        {
            get {
                return "";
            }
        }

        // 返回系统信息
        public sysinformation GetSystem
        {
            get
            {
                return sys;
            }
        }

        // 更新系统信息
        public void UpdateSystem(sysinformation value)
        {
            string tmp = " update information_table set value = '{0}' where [key]='{1}'; ";
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format(tmp, value.smtp_host, "smtp_host"));
            sb.Append(string.Format(tmp, value.smtp_password, "smtp_password"));
            sb.Append(string.Format(tmp, value.smtp_port, "smtp_port"));
            sb.Append(string.Format(tmp, value.send_email, "smtp_email"));
            sb.Append(string.Format(tmp, value.localhost, "exam_host"));
            sb.Append(string.Format(tmp, value.issend.ToStr(), "issend"));

            tmp = " update member_table set phone='{0}' where id={1}; ";
            sb.Append(string.Format(tmp, value.phone,value.adminid) );

            DbInstance up = DbManager.GetDbInstance();
            DbManager.BeginTransaction();
            try
            {
                up.ExecuteNonQueryBySql(sb.ToStr());
                DbManager.Commit();
            }
            catch (Exception ex)
            {
                DbManager.Rollback();
                throw ex;
            }
        }

        // 取得系统信息
        private void get_system_infromation()
        {
            DataTable dt = db.ExecuteDataTableBySql("select * from information_table where [key] like 'smtp_%' or [key]='exam_host' or [key]='issend' ");
            foreach (DataRow row in dt.Rows)
            {
                string key = row["key"].ToStr();
                switch (key)
                {
                    case "smtp_password":
                        sys.smtp_password = row["value"].ToStr();
                        break;
                    case "smtp_host":
                        sys.smtp_host = row["value"].ToStr();
                        break;
                    case "smtp_port":
                        sys.smtp_port = row["value"].ToInt();
                        break;
                    case "smtp_email":
                        sys.send_email = row["value"].ToStr();
                        break;
                    case "exam_host":
                        sys.localhost = row["value"].ToStr();
                        break;
                    case "issend":
                        sys.issend = Convert.ToBoolean(row["value"].ToStr());
                        break;
                }
            }
        }

        // 取得管理员信息
        private void get_administator_infromation()
        {
            DataTable dt = db.ExecuteDataTableBySql("select M_ID AS 'id', M_PHONE as 'phone', M_NAME AS 'name' from member_view WHERE R_POWER = 1");
            if (dt.Rows.Count > 0)
            {
                sys.adminid = dt.Rows[0]["id"].ToInt();
                sys.phone = dt.Rows[0]["phone"].ToStr();
                sys.adminname = dt.Rows[0]["name"].ToStr();
            }
            else throw new Exception("无法获取管理员信息.");
        }
    }
}
